package com.mysher.http_server

import android.hardware.Camera
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.SurfaceView
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import com.koushikdutta.async.BufferedDataSink
import com.koushikdutta.async.ByteBufferList
import okhttp3.*
import java.io.*
import java.net.*
import java.util.*
import java.util.concurrent.ArrayBlockingQueue
import java.util.concurrent.ThreadFactory
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit
import kotlin.math.sin


class MainActivity : AppCompatActivity(){

    companion object {
        private const val REQUEST_PERMISSIONS = 1
        private val APP_PERMISSIONS = arrayOf(
            "android.permission.CAMERA",
            "android.permission.RECORD_AUDIO"
        )

    }

    val threadFactory =
        ThreadFactory { r: Runnable ->
            Thread(
                r,
                "FileWrite"
            )
        }

    private var file: File? = null
    private var executors = ThreadPoolExecutor(1,1,5000,TimeUnit.MILLISECONDS,ArrayBlockingQueue<Runnable>(200),
        threadFactory)


    @RequiresApi(Build.VERSION_CODES.M)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val cameraHelper = CameraHelper(this@MainActivity,Camera.CameraInfo.CAMERA_FACING_FRONT,1280,720)
        val surfaceView = findViewById<SurfaceView>(R.id.surface_view)
        cameraHelper.setPreviewDisplay(surfaceView.holder)

        LocalHttpServer.get().startServer()
    }





    private fun writeFile(bytes: ByteArray) {
        try {
            val out = FileOutputStream(file,true) //指定写到哪个路径中
            out.write(bytes)
            out.flush()
            //Toast.makeText(this, "已保存", Toast.LENGTH_SHORT).show()
        } catch (e: FileNotFoundException) {
            e.printStackTrace()
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }

}